/**   
 * @Title: PngLoader.java 
 * @Package com.esun.util.pngloader
 * @Description: 图片加载
 * @author 程相亭 <br/>
 * @date 2012-03-22 下午05:07:33  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.util.pngloader;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

/**
 * com.esun.util.pngloader.PngLoader
 * 
 * @Description:图片加载类
 * @author 程相亭 <br/>
 * @date 2012-03-22 下午05:07:33
 */
public class PngLoader {

	private List<String> urlList = null;
	private static final int GET_PNG_SUCCESS = 10003;
	private static final int GET_PNG_FAIL = 10004;

	// 图片缓存
	private static Map<String, SoftReference<Drawable>> pngCache = null;

	public PngLoader() {

		urlList = Collections.synchronizedList(new ArrayList<String>());
		if (pngCache == null) {
			pngCache = new HashMap<String, SoftReference<Drawable>>();
		}
	}

	public void addToUrlList(String url) {

		this.urlList.add(url);
	}

	public Drawable getPng(final String url, final AsyPngLoader asyLoader) {
		System.out
				.println("pngCache----------------------------------------------"
						+ pngCache);
		// 先查询缓存
		if (pngCache.containsKey(url)) {
			// 缓存中有此图片
			SoftReference<Drawable> cachePng = pngCache.get(url);
			// 软引用的资源没有被回收
			if (cachePng.get() != null) {
				return cachePng.get();
			}
		}

		final Handler mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				switch (msg.what) {

				case GET_PNG_SUCCESS:

					// 设置图片
					asyLoader.pngLoadded((Drawable) msg.obj);
					break;

				case GET_PNG_FAIL:
					return;

				default:
					break;
				}
			}

		};

		// 获取队标线程
		new Thread() {

			@Override
			public void run() {
				super.run();

				// 否则下载图片
				Drawable png;
				try {

					png = loadPngFromUrl(url);
					if (png != null) {
						// 存入缓存
						pngCache.put(url, new SoftReference<Drawable>(png));

						Message msg = mHandler.obtainMessage(GET_PNG_SUCCESS,
								png);
						mHandler.sendMessage(msg);

					} else {
						Message msg = mHandler.obtainMessage(GET_PNG_FAIL);
						mHandler.sendMessage(msg);
					}

				} catch (IOException e) {
					Message msg = mHandler.obtainMessage(GET_PNG_FAIL);
					mHandler.sendMessage(msg);
				}
			}

		}.start();

		return null;
	}

	/**
	 * @throws IOException
	 * @Title: loadPngFromUrl
	 * @Description: 获取队标
	 * @param
	 * @return
	 * @throws
	 */
	public Drawable loadPngFromUrl(String url) throws IOException {

		Drawable png = null;

		URL fileURL = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) fileURL.openConnection();
		conn.setDoInput(true);
		conn.connect();
		InputStream is = conn.getInputStream();

		// Bitmap bitmap = BitmapFactory.decodeStream(is) ;
		png = Drawable.createFromStream(is, "src");
		// png = BitmapDrawable(bitmap) ;

		return png;
	}

	// 回调接口
	public interface AsyPngLoader {

		public void pngLoadded(Drawable drawable);

	}
}
